package com.hake.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hake.model.PetCompanyRegistBean;
import com.hake.model.PetOwnerRegistBean;
import com.hake.service.PetCompanyService;
import com.hake.util.ErrorInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2017/4/1.
 */

@RestController
@RequestMapping("/pet/company")
public class PetCompanyController {

    private static Logger logger = LoggerFactory.getLogger(PetCompanyController.class);

    @Autowired
    private PetCompanyService petCompanyService;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ModelAndView hello() {
        ModelAndView mv = new ModelAndView("hello");
        return mv;
    }

    @RequestMapping(value = "/add")
    public String df(@RequestParam("petCompanyRegistBean") String json) {

        ObjectMapper mapper = new ObjectMapper();
        PetCompanyRegistBean petCompanyRegistBean = null;
        try {
            petCompanyRegistBean = mapper.readValue(json, PetCompanyRegistBean.class);
        } catch (IOException e) {
            logger.info("hello world....");
            e.printStackTrace();
        }

        logger.info(petCompanyRegistBean.toString());

        String user_id = petCompanyRegistBean.getUser_id();
        String pet_company_name = petCompanyRegistBean.getPet_company_name();
        String pet_company_phone = petCompanyRegistBean.getPet_company_phone();
        String pet_legal_person_name = petCompanyRegistBean.getPet_legal_person_name();
        String pet_legal_person_phone = petCompanyRegistBean.getPet_legal_person_phone();
        String pet_company_card_type = petCompanyRegistBean.getPet_company_card_type();
        String pet_company_card_name = petCompanyRegistBean.getPet_company_card_name();
        String pet_company_place = petCompanyRegistBean.getPet_company_place();
        String pet_company_street = petCompanyRegistBean.getPet_company_street();
        String pet_business_license = petCompanyRegistBean.getPet_business_license();
        String pet_group_no = petCompanyRegistBean.getPet_group_no();
        String pet_legal_person_photo = petCompanyRegistBean.getPet_legal_person_photo();
        String pet_name = petCompanyRegistBean.getPet_name();
        String pet_type = petCompanyRegistBean.getPet_type();
        String pet_sex = petCompanyRegistBean.getPet_sex();
        String pet_birth = petCompanyRegistBean.getPet_birth();
        String pet_immune_no = petCompanyRegistBean.getPet_immune_no();
        String pet_immune_group = petCompanyRegistBean.getPet_immune_group();
        String pet_immune_able_date = petCompanyRegistBean.getPet_immune_able_date();
        String pet_birthday = petCompanyRegistBean.getPet_birthday();
        String pet_id_no = petCompanyRegistBean.getPet_id_no();
        String pet_police_station = petCompanyRegistBean.getPet_police_station();
        String pet_photo_positive = petCompanyRegistBean.getPet_photo_positive();
        String pet_photo_side = petCompanyRegistBean.getPet_photo_side();

        int result = petCompanyService.add(user_id,pet_company_name,pet_company_phone, pet_legal_person_name,pet_legal_person_phone,pet_company_card_type, pet_company_card_name, pet_company_place, pet_company_street, pet_business_license, pet_group_no, pet_legal_person_photo, pet_name, pet_type, pet_sex, pet_birth, pet_immune_no, pet_immune_group,pet_immune_able_date, pet_birthday, pet_id_no, pet_police_station, pet_photo_positive,pet_photo_side);

        ErrorInfo errorInfo = new ErrorInfo();
        if(result >= 1){
            errorInfo.setErrInfo("0","success");
        }else{
            errorInfo.setErrInfo(String.valueOf(result),"failed");
        }

        return JSON.toJSONString(errorInfo);
    }
}
